package org.wiztools.gaewebtester;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author subwiz
 */
public class AddServlet extends HttpServlet {

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/add.jsp").include(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<String> messages = new ArrayList<String>();
        List<String> errors = new ArrayList<String>();
        
        // URL:
        final String url = request.getParameter("url");
        if(url == null || !url.matches("^[a-zA-Z0-9]{3,5}$")) {
            errors.add("Url should be 3-5 alphanumeric characters.");
        }
        
        // Body:
        final String body = request.getParameter("body");
        if(body == null || body.trim().equals("")) {
            errors.add("Body is empty.");
        }
        
        // Set the data for pre-filled form:
        request.setAttribute("url", url);
        request.setAttribute("body", body);
        
        if(!errors.isEmpty()) {
            request.setAttribute("errors", errors);
            request.getRequestDispatcher("/WEB-INF/jsp/add.jsp")
                    .include(request, response);
            return;
        }
        
        // Save in cache:
        if(CacheUtil.get(url) != null) {
            messages.add("Overwritten existing content successfully!");
        }
        else {
            messages.add("Added successfully!");
        }
        CacheUtil.put(url, body);
        
        final String fullUrl = "/view/" + url;
        messages.add("View content: <a href='" + fullUrl + "'>" + fullUrl + "</a>.");
        messages.add("Url valid for: " + CacheUtil.TIMEOUT_HOURS + " hours.");
        
        request.setAttribute("messages", messages);
        
        request.getRequestDispatcher("/WEB-INF/jsp/add.jsp")
                .include(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
